﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Drawing;

public partial class Bidder_GoodDetail : System.Web.UI.Page
{
    Int64 GoodsId = -1;
    string strSql;
    public string straa;
    public string strbb;
    //public string choose;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumComapanyRG);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany); 
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))//是否为合法用户
        {
            if (!IsPostBack)
            {
                straa = "none";
                strbb = "none";
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                if (_Account.Sort == "0")
                {
                    choose.Visible = false;
                }
                else
                {
                    choose.Visible = true;
                }
                if (string.IsNullOrEmpty(Request.QueryString["id"]))//添加商品
                {
                    this.Title = "添加商品";
                    if (_Account.Sort == "0")
                    {
                        int _intProductAddStatus = 0;
                        string sql = "select CompanyName,ProductAddStatus  from Company where Accountid='" + _Account.AccountId + "'";
                        Db db = new Db();
                        System.Data.IDataReader dr = db.DataReader(sql);
                        while (dr.Read())
                        {
                            txtCompany.Text = dr[0].ToString();
                            _intProductAddStatus = SysFun.ToInt(dr["ProductAddStatus"].ToString());
                        }
                        txtIsSalerId.Value = _Account.AccountId.ToString();
                        dr.Close();
                        dr.Dispose();
                        db.Close();
                        if (_intProductAddStatus == 0)
                        {
                            Response.Write("<script language=JavaScript>alert('您暂时没有权限增加品种！如需帮助请于招标中心联系！！！');window.opener=null;window.open('','_self');window.close();</script>");
                        }
                    }
                    else
                    {
                        
                    }
                    txtAccountId_TJR.Text = _Account.AccountId.ToString();
                }
                else//修改商品
                {
                    this.Title = "修改商品";
                    if (HZ.Fun.IsInt(Request.QueryString["id"]))
                    {
                        GoodsId = Convert.ToInt64(Request.QueryString["id"]);
                    }
                    else
                    {
                        string _goodsid = MsgBox.JieMi_Decrypt(Request.QueryString["id"].ToString());
                        if (HZ.Fun.IsInt(_goodsid))
                        {
                            GoodsId = Convert.ToInt64(_goodsid);
                        }
                        else
                        {
                            HZ.Web.WebUtil.Alert("没有要添加的商品或页面过期，请重新点击！！");
                            Response.End();
                        }
                    }

                    Db db = new Db();
                    try
                    {
                        System.Data.IDataReader dr;
                        //labNoPass.Visible = false;
                        //labNoPass1.Visible = false;
                        //butUpdate.Visible = false;
                        strSql = "select GoodsByCompany.*,ca.CompanyName,cb.CompanyName as companyName_Agent,isnull(NoPass,'') as NoPass from GoodsByCompany inner join Company ca on GoodsByCompany.AccountId_SC=ca.AccountId left join Company cb on GoodsByCompany.AccountId_Agent=cb.AccountId  where GoodsByCompanyid ='" + GoodsId + "' ";
                        dr = db.DataReader(strSql);
                        while (dr.Read())
                        {
                          
                            rbtnYb.SelectedValue = dr["memo1"].ToString();
                            txtProductNameSM.Text = dr["ProductNameSM"].ToString();
                            txtAccountId_TJR.Text = dr["AccountId_TJR"].ToString();
                            //labProductCode.Text = dr["ProductCode"].ToString();
                            labProductName.Text = dr["productname"].ToString();
                            string _strMedicinemodel = dr["medicinemodel"].ToString();
                            string _strOutlookc = dr["outlookc"].ToString();
                            labMedicinemodel.Text = _strMedicinemodel;
                            labOutlookc.Text = _strOutlookc;
                            //if (_strMedicinemodel == "大输液" && _strOutlookc.IndexOf("玻瓶") == -1)
                            //{
                            //    Label4.Text = Label2.Text + "(大容量制剂（治疗用大输液）未标明包材的为玻瓶。)";
                            //    Label4.BackColor = Color.Yellow; ;
                            //}
                            //labOutlookc.Text = dr["outlookc"].ToString();
                            //labMedicinemodel.Text = dr["medicinemodel"].ToString();
                            labUnit.Text = dr["unit"].ToString();
                            txtCompany.Text = dr["companyName"].ToString();
                            txtFactor.Text = dr["xishu"].ToString();
                            //txtFactor.Enabled = false;
                            txtGoodsId.Text = dr["GoodsByCompanyid"].ToString();
                            txtGoodsInstruction.Text = dr["GoodsInstruction"].ToString();
                            txtGoodsName.Text = dr["goodsname"].ToString();
                            txtImportCode.Text = dr["ImportCode"].ToString();
                            txtImportRegistDate.Text = dr["ImportRegistDate"].ToString();
                            if (SysFun.ToTrim(dr["ImportRegistDate"]) == "1900-1-1 0:00:00")
                            {
                                txtImportRegistDate.Text = "";
                            }
                            txtLicenseNumber.Text = dr["licensenumber"].ToString();
                            txtLicenseNumberDate.Text = dr["licensenumberdate"].ToString();
                            if (SysFun.ToTrim(dr["licensenumberdate"]) == "1900-1-1 0:00:00")
                            {
                                txtLicenseNumberDate.Text = "";
                            }
                            txtMaterial.Text = dr["Material"].ToString();
                            txtPackUnit.Text = dr["PackUnit"].ToString(); 
                            rdoQualityLevels.SelectedValue = SysFun.ToTrim(dr["QualityLevels"]);
                            if (rdoQualityLevels.SelectedValue == "0") lableshow.Text = "第一质量层次： 专利保护期内的化合物专利药品和药物组合物专利药品；获得国家科技进步奖的药品";
                            if (rdoQualityLevels.SelectedValue == "1") lableshow.Text = "第二质量层次： 发改委公布的单独定价药品和优质优价中成药";
                            if (rdoQualityLevels.SelectedValue == "2") lableshow.Text = "第三质量层次： 专利保护期内的工艺流程发明专利药品（含剂型专利药品）；过保护期的（化合物、药物组合物）专利药品；进口药品；获得美国ＦＤＡ认证或欧盟ＣＥ、ＣＧＭＰ认证的药品；质量标准起草单位的药品（含原研药品和首仿国外药品）";
                            if (rdoQualityLevels.SelectedValue == "3") lableshow.Text = "第四质量层次： 通过GMP认证的其他药品";
                          
                            txtRetailPrice.Text = dr["RetailPrice"].ToString();
                            //labNoPass.Text = dr["nopass"].ToString();

                            if (SysFun.ToTrim(dr["IsImport"]) != "")
                            {
                                chkIsImport.Checked = Convert.ToBoolean(dr["IsImport"].ToString());
                            }
                            else
                            {
                                chkIsImport.Checked = false;
                            }
                            if (chkIsImport.Checked == false)
                            {
                                straa = "none";
                            }
                            else
                            {
                                straa = "block";
                            }

                            if (dr["IsSurveyReport"].ToString() != "")
                            {
                                chkIsSurveyReport.Checked = Convert.ToBoolean(dr["IsSurveyReport"].ToString());
                            }
                            else
                            {
                                chkIsSurveyReport.Checked = false;
                            }
                            if (chkIsSurveyReport.Checked == false)
                            {
                                strbb = "none";
                            }
                            else
                            {
                                strbb = "block";
                                txtSurveyReportDate.Text = dr["SurveyReportDate"].ToString();
                                if (SysFun.ToTrim(dr["SurveyReportDate"]) == "1900-1-1 0:00:00")
                                {
                                    txtSurveyReportDate.Text = "";
                                }
                                txtSurveyReportId.Text = dr["SurveyReportId"].ToString();
                            }

                            this.txtjz.Text = dr["jzdate"].ToString();
                            txtIsalerId_dL.Text = dr["AccountId_Agent"].ToString();
                            txtIsSalerId.Value = dr["AccountId_SC"].ToString();
                            txtCompany_dl.Text = dr["companyName_Agent"].ToString();
                            txtMemo4.Text = SysFun.ToTrim(dr["memo4"]);
                            txtMemo5.Text = dr["memo5"].ToString();
                            if (dr["checkstatus"] != "" && (dr["checkstatus"].ToString() == "1" || dr["checkstatus"].ToString() == "3"))
                            {
                                pnl.Enabled = false;
                            }
                            else
                            {
                                LNopass.Text = dr["NoPass"].ToString();
                            }
                            //查看
                            if (!string.IsNullOrEmpty(Request.QueryString["flag"]))
                            {
                                pnl.Enabled = false;
                                this.Title = "查看商品";
                                lbltitle.Text = "查看商品";
                                choose.Visible = false;
                                Div1.Visible = false;
                            }
                        }

                        dr.Close();
                        dr.Dispose();
                        
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        db.Close();
                    }

                }
            }
        }
    }

    protected void BtnSave_Click(object sender, EventArgs e)
    {
        //add can not edit 2008.8.6  by wushuang
        //增加只要是审核过的(包括通过与不通过的都不能修改)
        /*
      
        if (ViewState["status"].ToString().Equals("2"))
        {
            Response.Write("<script language='javascript'>alert('状态已被审核为未通过，不能修改！');</script>");
            return;
        } */
        //if (ViewState["status"].ToString().Equals("1"))
        //{
        //    Response.Write("<script language='javascript'>alert('数据已审核通过，不能修改！');</script>");
        //    return;
        //}
        if (CompanySave("0") == true)
        {
            Response.Write("<script>alert('保存成功！！！');window.open('','_self');window.close();</script>");
        }



    }

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close();</script>");
    }

    public bool CompanySave(string strMemo3)
    {
        //if (ViewState["status"].ToString().Equals("3"))
        //{
        //    Response.Write("<script language='javascript'>alert('状态为原始状态，不能修改！');</script>");
        //    return false;
        //}

        Db db = new Db(false);
        HZ.MBSM.BLL.Goods userGoodsUpdate = new HZ.MBSM.BLL.Goods();
        HZ.MBSM.Model.Goods goods = new HZ.MBSM.Model.Goods();
        if (goods != null)
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {


                int rowCun = 0;
                if (txtGoodsId.Text == "-1")//新建
                {
                    Db db1 = new Db();
                    System.Data.IDataReader dr;
                    strSql = "select count(GoodsByCompanyId) as cun from GoodsByCompany where productname='" + SysFun.ToTrim(labProductName.Text) + "' and MedicineModel='" + SysFun.ToTrim(labMedicinemodel.Text) + "' and Outlookc='" + SysFun.ToTrim(labOutlookc.Text) + "' and Unit='" + SysFun.ToTrim(labUnit.Text) + "' and Material='" + SysFun.ToTrim(txtMaterial.Text) + "'  and xishu= '" + SysFun.ToTrim(SysFun.ToInt(txtFactor.Text)) + "' AND ACCOUNTID_SC='" + SysFun.ToTrim(SysFun.ToLong(txtIsSalerId.Value.ToString())) + "' ";
                    dr = db1.DataReader(strSql);
                    if (dr.Read())
                    {
                        rowCun = SysFun.ToInt(dr["cun"]);
                    }
                    dr.Close();
                    dr.Dispose();
                    db1.Close();
                }
                else//修改
                {
                    Db db1 = new Db();
                    System.Data.IDataReader dr;
                    strSql = "select count(GoodsByCompanyId) as cun from GoodsByCompany where productname='" + SysFun.ToTrim(labProductName.Text) + "' and MedicineModel='" + SysFun.ToTrim(labMedicinemodel.Text) + "' and Outlookc='" + SysFun.ToTrim(labOutlookc.Text) + "' and Unit='" + SysFun.ToTrim(labUnit.Text) + "'  and Material='" + SysFun.ToTrim(txtMaterial.Text) + "' and xishu= '" + SysFun.ToTrim(SysFun.ToInt(txtFactor.Text)) + "' AND ACCOUNTID_SC='" + SysFun.ToTrim(SysFun.ToLong(txtIsSalerId.Value.ToString())) + "' and GoodsByCompanyId<>'" + SysFun.ToTrim(txtGoodsId.Text) + "'";
                    dr = db1.DataReader(strSql);
                    if (dr.Read())
                    {
                        rowCun = SysFun.ToInt(dr["cun"]);
                    }
                    dr.Close();
                    dr.Dispose();
                    db1.Close();
                }
                if (rowCun > 0)
                {
                    Response.Write("<script language=JavaScript>alert('该商品在数据库中已经存在！！！');</script>");
                    return false;
                }
                else
                {
                    //是否进口
                    string IsSalerId;
                    if (chkIsImport.Checked)
                    {
                        if (SysFun.ToTrim(Request.Form["txtIsSalerId_dl1"]) == "")
                        {
                            IsSalerId = SysFun.ToTrim(txtIsalerId_dL.Text.ToString());
                        }
                        else
                        {
                            IsSalerId = SysFun.ToTrim(Request.Form["txtIsSalerId_dl1"]);
                        }

                        goods.ImportCode = SysFun.ToTrim(txtImportCode.Text);
                        goods.AccountId_Agent = SysFun.ToLong(IsSalerId);
                        if (txtImportRegistDate.Text != "")
                            goods.ImportRegistDate = Convert.ToDateTime(txtImportRegistDate.Text.ToString());
                        else
                            goods.ImportRegistDate = Convert.ToDateTime("1900-1-1");
                    }
                    else
                    {
                        goods.ImportCode = "";
                        goods.AccountId_Agent = SysFun.ToLong(-1);
                        goods.ImportRegistDate = Convert.ToDateTime("1900-1-1");
                    }

                    goods.memo1 = rbtnYb.SelectedIndex;
                    goods.ProductName = labProductName.Text.Trim();
                    goods.MedicineModel = labMedicinemodel.Text.Trim();
                    goods.Outlookc = labOutlookc.Text.Trim();
                    goods.Unit = labUnit.Text.Trim();
                    goods.Factor = Convert.ToInt32(txtFactor.Text.ToString());

                    goods.GoodsId = Convert.ToInt64(txtGoodsId.Text);
                    goods.AccountId_SC = SysFun.ToLong(txtIsSalerId.Value.ToString());
                    goods.AccountId_TJR = SysFun.ToLong(txtAccountId_TJR.Text);
                    //goods.CheckStatus = hidCheckStatus.Value;
                    //if (hidShenHeTime.Value != "")
                    //    goods.shenhetime = Convert.ToDateTime(hidShenHeTime.Value);
                    //else
                    //    goods.shenhetime = Convert.ToDateTime("1900-1-1");

                    goods.GoodsInstruction = txtGoodsInstruction.Text.ToString();
                    goods.GoodsName = txtGoodsName.Text.ToString();

                    //有无药品检验报告
                    goods.IsImport = chkIsImport.Checked;
                    goods.IsSurveyReport = chkIsSurveyReport.Checked;
                    if (goods.IsSurveyReport)
                    {
                        if (txtSurveyReportDate.Text != "")
                            goods.SurveyReportDate = Convert.ToDateTime(txtSurveyReportDate.Text.ToString());
                        else
                            goods.SurveyReportDate = Convert.ToDateTime("1900-1-1");
                        goods.SurveyReportId = txtSurveyReportId.Text.ToString();
                    }
                    else
                    {
                        goods.SurveyReportDate = Convert.ToDateTime("1900-1-1");
                        goods.SurveyReportId = "";
                    }

                    goods.LicenseNumber = txtLicenseNumber.Text.ToString();
                    if (txtLicenseNumberDate.Text != "")
                        goods.LicenseNumberDate = Convert.ToDateTime(txtLicenseNumberDate.Text.ToString());
                    else
                        goods.LicenseNumberDate = Convert.ToDateTime("1900-1-1");

                    goods.Material = txtMaterial.Text.ToString();
                    goods.Pack = labOutlookc.Text.ToString() + "×" + txtFactor.Text.ToString() + labUnit.Text.ToString() + "/" + txtPackUnit.Text.ToString();
                    goods.PackUnit = txtPackUnit.Text.ToString();
                    //if (labProductId.Text != "")
                    //    goods.ProductId = Convert.ToInt64(labProductId.Text.ToString());
                    goods.QualityLevels = SysFun.ToTrim(rdoQualityLevels.SelectedValue);
                    if (txtRetailPrice.Text != "")
                        goods.RetailPrice = Convert.ToDouble(txtRetailPrice.Text.ToString());
                    //goods.memo3 = strMemo3.ToString();
                    goods.memo4 = SysFun.ToTrim(txtMemo4.Text);
                    goods.memo5 = SysFun.ToTrim(txtMemo5.Text);
                    goods.ImgUrl = "";
                    //goods.ImgUrl = SysFun.ToTrim(txtImgUrl.Text);
                    //goods.NoPass = labNoPass.Text;
                    goods.ProductNameSM = txtProductNameSM.Text;
                    //goods.GmpCode = this.txtGmpCode.Text.Trim();
                    //if (txtGmpValDate.Text != "")
                    //{
                    //    goods.GmpValDate = Convert.ToDateTime(this.txtGmpValDate.Text.Trim());

                    //}
                    //else
                    //{

                    //    goods.GmpValDate = Convert.ToDateTime("1900-1-1");
                    //}
                    goods.Jzdate = this.txtjz.Text.ToString();
                    if (userGoodsUpdate.GoodsByCompanyEdit(goods))
                    {
                        if (goods.GoodsId == -1)
                        {
                            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "新建", "GoodsByCompany", "userGoodsUpdate.GoodsByCompanyEdit(goods)" + goods.GoodsId.ToString(), "../Company/GoodsList.aspx");
                        }
                        else
                        {
                            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "GoodsByCompany", "userGoodsUpdate.GoodsByCompanyEdit(goods)" + goods.GoodsId.ToString(), "../Company/GoodsList.aspx");
                        }
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        else
        {
            Response.Write("<script language=JavaScript>alert('您无权修改商品！！！');</script>");
            return false;
        }
        return false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (CompanySave("1") == true)
            Response.Write("<script>alert('提交成功！！！');</script>");
        Response.Write("<script>window.opener.location.reload();window.close();</script>");


    }
    protected void butUpdate_Click(object sender, EventArgs e)
    {

        if (CompanySave("2") == true)
            Response.Write("<script>alert('保存成功！！！');</script>");
        Response.Write("<script>window.opener.location.reload();window.close();</script>");
    }
}
